package smtp;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Scanner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

import smtp.MainScreenController.ButtonListener;

/**
 * This is the main screen of the application, containing three major parts:
 * - The folder list on the left
 * - The message list in the middle
 * - And the currently selected message on the right
 * 
 * All of these areas are manipulated by the MainScreenController
 * @author wendlandts, chandlerr, liw
 *
 */
public class MainScreen extends JFrame{
	MainScreenController ctrl=null;


	private static int WIDTH = 1400;
	private static int HEIGHT = 600;

	// Buttons
	private JButton composeButton = new JButton("Compose");
	private JButton replyButton = new JButton("Reply");
	private JButton replyAllButton = new JButton("Reply All");
	private JButton forwardButton = new JButton("Forward");
	// The list for the messages
	private JList boxList = new JList();
	// The right side of the screen (Message Viewing)
	private JPanel messageViewPanel = new JPanel();
	private JLabel to = new JLabel();
	private JLabel real = new JLabel();
	private JLabel cc = new JLabel();
	private JLabel bcc = new JLabel();
	private JLabel from = new JLabel();
	private JLabel flags = new JLabel();
	private JLabel subject = new JLabel();
	private JTextArea viewMessageArea = new JTextArea();
	private JLabel attachments = new JLabel();
	private JButton downloadAttachments = new JButton("Download Attachments");



	private JScrollPane viewMessageAreaScroll = new JScrollPane(viewMessageArea);


	private JPanel messageListPanel;
	private JSplitPane browserPane;
	private JSplitPane mainPane;

	/**
	 * Accepts a MainScreenController so it can set the listeners in this class to the listeners in the controller's class
	 * @param ctrl the controller for this screen
	 */
	public MainScreen(MainScreenController ctrl) {
		this.ctrl=ctrl;
	}

	/**
	 * Sets up the main screen UI
	 */
	public void setUpUI() {
		// Set up main frame
		this.setSize(WIDTH, HEIGHT);
		this.setLocation(500, 250);
		this.setTitle("Email Client");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new BorderLayout());

		// Add listeners to buttons
		ButtonListener listener =ctrl.new ButtonListener();

		composeButton.addActionListener(listener);
		composeButton.setActionCommand("Compose");

		replyButton.addActionListener(listener);
		replyButton.setActionCommand("Reply");

		replyAllButton.addActionListener(listener);
		replyAllButton.setActionCommand("Reply All");

		downloadAttachments.addActionListener(listener);
		downloadAttachments.setActionCommand("Download Attachments");

		forwardButton.addActionListener(listener);
		forwardButton.setActionCommand("Forward");

		// --- BUTTONS ----
		//COMPOSE
		JToolBar composeTools = new JToolBar();
		composeTools.setFloatable(false);
		composeTools.add(composeButton);


		// REPLY, REPLY ALL, FORWARD
		// The right half of the upper panel
		// For the Reply and Reply All buttons
		JPanel replyPanel = new JPanel();
		replyPanel.setLayout(new GridLayout(1,3));

		// Place the Reply and Reply All buttons on the upper right panel
		replyPanel.add(replyButton);
		replyPanel.add(replyAllButton);
		replyPanel.add(forwardButton);

		// MESSAGE OP PANEL: DELETE, MARK READ
		JPanel messageOpPanel = new JPanel();
		messageOpPanel.setLayout(new GridLayout(1,3));
		messageOpPanel.add(new JButton("Delete"));
		messageOpPanel.add(new JButton("Mark Unread"));

		// --- BROWSER ---
		// messagesPanel inside browserPanel inside this		
		browserPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, boxList, messageListPanel);
		browserPane.setDividerSize(4);
		browserPane.setMinimumSize(new Dimension(700, 10));

		// --- MSG VIEWER ---
		JPanel readMsgPanel = new JPanel();
		readMsgPanel.setLayout(new BorderLayout());
		readMsgPanel.add(replyPanel, BorderLayout.NORTH); // add reply, reply all, forward

		// Message viewing
		messageViewPanel.setLayout(new BorderLayout());
		JPanel messageInfoPanel = new JPanel();
		messageInfoPanel.setLayout(new GridLayout(9,1));

		messageInfoPanel.add(from);
		messageInfoPanel.add(real);
		messageInfoPanel.add(to);
		messageInfoPanel.add(cc);
		messageInfoPanel.add(bcc);
		messageInfoPanel.add(subject);
		messageInfoPanel.add(attachments);
		messageInfoPanel.add(flags);
		messageInfoPanel.add(downloadAttachments);
		messageViewPanel.add(messageInfoPanel, BorderLayout.NORTH);
		messageViewPanel.add(viewMessageAreaScroll, BorderLayout.CENTER);
		viewMessageArea.setEditable(false);
		messageViewPanel.setVisible(false);

		readMsgPanel.add(messageViewPanel);

		// --- MAIN SPLIT PANE ---
		mainPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, browserPane, readMsgPanel);
		mainPane.setOpaque(true);

		// --- THIS ---
		this.add(mainPane, BorderLayout.CENTER);
		this.add(composeTools, BorderLayout.NORTH);

		//this.setVisible(true);
	}

	public void popUpError(String errorMsg) {
		JOptionPane.showMessageDialog(null, errorMsg);
	}

	public void updateMessageView(JPanel panel) {
		if(!messageViewPanel.isVisible()) {
			messageViewPanel.setVisible(true);
		}
		messageViewPanel = panel;
		validate();
	}

	/**
	 * Finds the selected message and displays it to the right side of the screen
	 */
	public void updateMessage(ReceivedEmailParser message) {

		if(!messageViewPanel.isVisible()) {
			messageViewPanel.setVisible(true);
		}
		
		from.setText("From: " + message.getFrom());
		real.setText("Real Sender: "+ message.getRealSender());
		to.setText("To: " + message.getAddress(RecipientType.TO));
		cc.setText("CC: " + message.getAddress(RecipientType.CC));
		bcc.setText("BCC: " + message.getAddress(RecipientType.BCC));
		flags.setText("Current Flags: " + message.getSystemFlags());
		subject.setText("Subject: " + message.getSubject());
		viewMessageArea.setText(message.getContent());

		try {
			attachments.setText("Attached: " + message.getAttachName());
			downloadAttachments.setText(message.getAttachName().equalsIgnoreCase("No attachment")?"No attachment":"Download:"+message.getAttachName());
		} catch (MessagingException e) {
			System.out.println("Could not get attached files");
			e.printStackTrace();
		}
		validate();
	}

	/**
	 * Used to pass up the boxList from this window to the controller
	 * @return The list of messages
	 */
	public JList getBoxList(){
		return boxList;
	}

	/**
	 * Used to set the panel where the message are
	 * @param panel
	 */
	public void setMessageListPanel(JPanel panel){
		messageListPanel = panel;
		browserPane.setRightComponent(panel);
		validate();
	}
}



